package com.puris.servlet.student;

import com.puris.service.student.Service;
import com.puris.service.student.ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author Sonata
 * @data 2021/11/24 18:28
 */
public class ExitCompetitionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = (String) req.getSession().getAttribute("id");

        Service service = new ServiceImpl();

        boolean flag = false;

        try {
            flag = service.exitCompetition(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        req.getSession().setAttribute("competitionName", null);

        if (flag) {
            req.setAttribute("exit", "退出成功！");
        }
        req.getRequestDispatcher("jsp/student/joinCompetition.jsp").forward(req, resp);
    }
}
